package com.veterinaria.dao;

import java.io.Reader;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.veterinaria.bean.EmpleadoBean;
import com.veterinaria.bean.PacienteBean;
import com.veterinaria.services.PacienteService;

public class PacienteDAO implements PacienteService{
	SqlSessionFactory sqlmapper= null;{
		try {
		   	 Reader reader = Resources.getResourceAsReader("ConfiguracionIbatis.xml");
		   	 sqlmapper =new  SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			 e.printStackTrace();
		}
	}

	@Override
	public ArrayList<PacienteBean> listarPaciente() throws Exception {
		ArrayList<PacienteBean> lista = null;
		SqlSession session = sqlmapper.openSession();
		try {
			lista = (ArrayList<PacienteBean>)session.selectList("pacientexml.listaPaciente");
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			session.close();
		}
		return lista;
	}

	@Override
	public ArrayList<PacienteBean> listarPacienteNombre(PacienteBean obj)
			throws Exception {
		ArrayList<PacienteBean> lista = null;
		SqlSession session = sqlmapper.openSession();
		try {
			lista = (ArrayList<PacienteBean>)session.selectList("pacientexml.listaPacienteNombre",obj);

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			session.close();
		}
		return lista;
	}

	@Override
	public PacienteBean buscaPaciente(String codPaciente) throws Exception {
		PacienteBean bean=null;
		 SqlSession session = sqlmapper.openSession();			
			try {				
				bean= (PacienteBean) session.selectOne("pacientexml.buscaPaciente",codPaciente);				
			} catch (Exception e) {
				e.printStackTrace();
			}finally{ session.close();}
		return bean;
	}

	@Override
	public void ModificarPaciente(PacienteBean obj) throws Exception {
		// TODO Auto-generated method stub
		String msg="";
		SqlSession session = sqlmapper.openSession();
		try {
			msg= "Resultado: "+session.insert("pacientexml.modificaPaciente",obj);
			session.commit();
		} catch (Exception e) {
			e.getMessage();
			session.rollback();
		}finally{
			session.close();
		}
	}

	@Override
	public String EliminarPaciente(PacienteBean obj) throws Exception {
		String msg="";
		SqlSession session = sqlmapper.openSession();
		try {
			msg= "Resultado: "+session.delete("pacientexml.eliminaPaciente",obj);
			session.commit();
		} catch (Exception e) {
			e.getMessage();
		}finally{
			session.close();
		}
		return msg;
	}

	@Override
	public PacienteBean obtenerPaciente(PacienteBean paciente) throws Exception {
		PacienteBean bean=null;
		SqlSession session = sqlmapper.openSession();			
		try {				
			bean= (PacienteBean) session.selectOne("pacientexml.obtenerPaciente",paciente);				
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ session.close();}
		return bean;
	}

	@Override
	public void grabarPaciente(PacienteBean obj) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session = sqlmapper.openSession();
		String msg="";
		try {
			msg="exito"+ session.insert("pacientexml.insertarPaciente",obj);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			msg=e.getMessage();
			session.rollback();
		}finally{
			session.close();
		}
	}

	@Override
	public int obtenerCodigo() throws Exception {
		// TODO Auto-generated method stub
		SqlSession session = sqlmapper.openSession();
		int codigo=0;
		try {				
			codigo = (int)session.selectOne("pacientexml.obtenerCodigo");				
		} catch (Exception e) {
			e.printStackTrace();
		}finally{ session.close();}
	return codigo;
	}

}
